package com.dao;

import com.util.dbUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class userDao {



    public ResultSet checkUser(int userid, String password) throws SQLException, ClassNotFoundException {
        Statement statement = dbUtil.getStatement();
        String sql="select  *  from shopping.user where userid="+userid+" and password='"+password+"'";
        ResultSet resultSet = statement.executeQuery(sql);
        return resultSet;
    }

    public ResultSet checkUserExist(int userid) throws SQLException, ClassNotFoundException {
        Statement statement = dbUtil.getStatement();
        String sql="select  *  from shopping.user where userid="+userid;
        ResultSet resultSet = statement.executeQuery(sql);
        return resultSet;
    }



    public boolean  insertUser(int userid,String userName,String password,String address) throws SQLException, ClassNotFoundException {
        Statement statement = dbUtil.getStatement();
        String sql="insert  into shopping.user(userid,username,password,address) values ("+userid+",'"+userName+"','"+password+"','"+address+"')";
        int res = statement.executeUpdate(sql);
        if(res==1){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 交易成功就扣除用户费用
     * @param userid
     * @param decrMoney   减少的余额
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public void decrUserMoney(int userid,double decrMoney) throws SQLException, ClassNotFoundException {
        Statement statement = dbUtil.getStatement();
        String sql="update shopping.user set money=money-"+(decrMoney);
        statement.executeUpdate(sql);
    }


}
